<template>
  <div class="firmware">
    <div class="firmware-top">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleFirmwareUpload"
        >上传固件</el-button
      >
    </div>
    <el-table :data="firmwareList">
      <el-table-column label="版本号" sortable align="center" prop="versionNo" />
      <el-table-column label="OTA 文件" align="center">
        <template slot-scope="scope">
          <p>
            {{ Number(scope.row.otaLen / 1024 || 0).toFixed(2) }}KB
            <el-link type="primary">{{ getLastPartOfUrl(scope.row.otaUrl) }}</el-link>
          </p>
        </template>
      </el-table-column>
      <el-table-column label="镜像文件" align="center">
        <template slot-scope="scope">
          <p>
            {{ Number(scope.row.imgLen / 1024 || 0).toFixed(2) }}KB
            <el-link type="primary">{{ getLastPartOfUrl(scope.row.imgUrl) }}</el-link>
          </p>
        </template>
      </el-table-column>
      <el-table-column label="版本状态" align="center" prop="brandName" />
      <el-table-column label="创建时间" sortable align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <firmware-upload
      :open="open"
      @cancel="cancel"
      @getList="getFirmwareListFn"
      :id="id"
      ref="child"
    />
  </div>
</template>

<script>
import firmwareUpload from "./firmware-upload";
import { getFirmwareList, delFirmware } from "@/api/login";
export default {
  props: {
    id: {
      type: [Number, String],
      default: "",
    },
  },
  data() {
    return {
      open: false,
      firmwareList: [],
    };
  },
  components: {
    firmwareUpload,
  },
  methods: {
    async getFirmwareListFn() {
      try {
        const { id } = this;
        const { data } = await getFirmwareList(id);
        this.firmwareList = data || [];
      } catch (error) {
        console.error("获取固件管理列表失败", error);
      }
    },
    async handleDelete(row) {
      const { id } = row;
      try {
        const flag = await this.$modal.confirm("是否确认删除该数据项？");
        if (!flag) return;
        await delFirmware(id);
        this.getFirmwareListFn();
        this.$modal.msgSuccess("操作成功");
      } catch (error) {
        console.error("操作失败", error);
      }
    },
    cancel() {
      console.log(111);
      this.open = false;
      this.rest();
    },
    rest() {
      this.$refs.child.resetFormFun();
    },
    handleFirmwareUpload() {
      this.rest();
      this.open = true;
    },
    getLastPartOfUrl(url) {
      // 使用split方法将URL按斜杠分割成数组
      const parts = url.split("/");
      // 获取数组中最后一个元素
      return parts[parts.length - 1];
    },
  },
  created() {
    this.getFirmwareListFn();
  },
};
</script>

<style scoped lang="scss">
.firmware {
  margin: 0 0 22px 0;
  .firmware-top {
    margin-bottom: 22px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
}
</style>
